$(function() {
    var p = {
        pagenum: 1,
        pagesize: 2,
        cate_id: '',
        state: ''
    }

    listinit();
    cateinit();


    function listinit() {
        $.ajax({
            type: 'GET',
            url: '/my/movie/list',
            data: p,
            success: function(res) {
                if (res.status != 0) {
                    return layui.layer.msg('获取电影列表失败!');
                }
                var htmlStr = template('tql-list', res);
                $('tbody').html(htmlStr);

                pagerender(res.total);
            }
        })
    }

    function filterlist() {
        $.ajax({
            type: 'POST',
            url: '/my/movie/filterlist',
            data: p,
            success: function(res) {
                if (res.status != 0) {
                    return layui.layer.msg('获取电影列表筛选信息失败!');
                }
                var htmlStr = template('tql-list', res);
                $('tbody').html(htmlStr);

                pagerenderSecond(res.total);
                //layui.form.render();
                //console.log(htmlStr);

            }
        })
    }

    function pagelist() {
        $.ajax({
            type: 'POST',
            url: '/my/movie/pagelist',
            data: p,
            success: function(res) {
                if (res.status != 0) {
                    return layui.layer.msg('获取电影列表筛选信息失败!');
                }
                var htmlStr = template('tql-list', res);
                $('tbody').html(htmlStr);
            }
        })
    }



    template.defaults.imports.dateFormat = function(date) {
        var dt = new Date(date);

        var y = dt.getFullYear();
        var m = padZero(dt.getMonth() + 1);
        var d = padZero(dt.getDate());
        var hh = padZero(dt.getHours());
        var mm = padZero(dt.getMinutes());
        var ss = padZero(dt.getSeconds());

        return y + '-' + m + '-' + d + ' ' + hh + ':' + mm + ':' + ss;
    }

    function padZero(n) {
        return n > 9 ? n : '0' + n;
    };

    function cateinit() {
        $.ajax({
            type: 'GET',
            url: '/my/movie/cate',
            success: function(res) {
                if (res.status != 0) {
                    return layui.layer.msg('获取电影分类信息失败!');
                }
                var htmlStr = template('tql-cate', res.data);
                $('[name=cate_id]').html(htmlStr);
                //console.log(htmlStr);
                layui.form.render();
            }
        })
    };


    $('#search').on('submit', function(e) {
        e.preventDefault();
        var cateid = $('[name="cate_id"]').val();
        var state = $('#search_state').val();
        p.cate_id = cateid;
        p.state = state;
        filterlist();

    });


    function pagerender(n) {
        layui.laypage.render({
            elem: 'pagelist',
            count: n,
            limit: n,
            curr: p.pagenum,
            layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
            limits: [2, 4, 6, 8, 10],
            jump: function(obj, first) {
                p.pagenum = obj.curr;
                p.pagesize = obj.limit;
                if (!first) {
                    pagelist();
                }
            }
        })
    };


    function pagerenderSecond(n) {
        layui.laypage.render({
            elem: 'pagelist',
            count: n,
            limit: n,
            curr: p.pagenum,
            layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
            limits: [2, 4, 6, 8, 10],
            jump: function(obj, first) {
                p.pagenum = obj.curr;
                p.pagesize = obj.limit;
                //console.log(obj.limit);
                if (!first) {
                    pagelistSecond();

                }
            }
        })
    };

    function pagelistSecond() {
        $.ajax({
            type: 'POST',
            url: '/my/movie/filterpage',
            data: p,
            success: function(res) {
                if (res.status != 0) {
                    return layui.layer.msg('二次失败！');
                }
                var htmlStr = template('tql-list', res);
                $('tbody').html(htmlStr);

            }
        })
    }


})